<?php
if (isset($_POST['editer'])) 
{
	$req = $bdd->prepare('SELECT * FROM categories_creations WHERE id = :id');
	$req->execute(array('id' => $_POST['id']));
	$donnees = $req->fetch();
	?>
	<h1>Editer une catégorie d'article</h1>
	<form method="post" action="index.php?page=admin_categories_creations" enctype="multipart/form-data">
		<table border="0">
			<tr><td><label for="titre">Nom</label></td>
			<td><input type="text" name="nom" value="<?php echo stripslashes($donnees['nom']); ?>" maxlength="250" size="50" required></td></tr>
			<tr><td><label for="image">Image</label></td><td colspan="2"><input type="file" size="150" name="image" /></td></tr>
			<tr><td colspan="2"><label for="description">Description</label></td></tr>
			<tr><td colspan="2"><textarea id="description" name="description" rows="15" cols="80"><?php echo $donnees['description']; ?></textarea></td></tr>
			<tr><td colspan="2"><input type="hidden" name="id" value="<?php echo $donnees['id']; ?>" />
			<input type="submit" value="Modifier" name="modifier" class="bouton" /></td></tr>
		</table>
	</form>
	<?php
}
elseif (isset($_POST['modifier'])) 
{
	$req = $bdd->prepare('UPDATE categories_creations SET nom=:nom, description=:description WHERE id=:id');
	$req->execute(array(
		'nom' => stripslashes($_POST['nom']),
		'description' => stripslashes($_POST['description']),
		'id' => $_POST['id']
	));
	if (isset($_FILES['image']) AND $_FILES['image']['error'] == 0)
	{
		if ($_FILES['image']['size'] <= 1000000)
		{
			$infosfichier = pathinfo($_FILES['image']['name']);
			$extension_upload = $infosfichier['extension'];
			$extensions_autorisees = array('jpg', 'jpeg', 'gif', 'png');
			if (in_array($extension_upload, $extensions_autorisees))
			{
				$chemin = 'creations/'.basename($_FILES['image']['name']);
				echo $chemin;
				move_uploaded_file($_FILES['image']['tmp_name'], $chemin);
				$req = $bdd->prepare('UPDATE categories_creations SET image=:image WHERE id=:id');
				$req->execute(array(
					'image' => $chemin,
					'id' => $_POST['id']
				));
			}
		}
	}
	header('Location: index.php?page=admin_categories_creations'); 
}
elseif (isset($_POST['effacer'])) 
{
	$req = $bdd->prepare('DELETE FROM categories_creations WHERE id=:id');
	$req->execute(array(
			'id' => $_POST['id']
	));
	header('Location: index.php?page=admin_categories_creations'); 
}
elseif (isset($_POST['ajouter'])) 
{
	$req = $bdd->prepare('INSERT INTO categories_creations (nom, description) VALUES (:nom, :description)');
	$req->execute(array(
		'nom' => stripslashes($_POST['nom']),
		'description' => stripslashes($_POST['description'])
	));
	header('Location: index.php?page=admin_categories_creations'); 
}
else
{
	?>
	<h1 id="titre">Gestion des types de ressources</h1>
	<table border="0" width="100%">
		<?php
		$req = $bdd->query('SELECT * FROM categories_creations ORDER BY nom');
		while ($donnees = $req->fetch())
		{
			?>
			<form method="post" action="index.php?page=admin_categories_creations">
				<tr>
				<td width="5%"><img src="<?php echo $donnees['image']; ?>" width="30" /></td>
				<td width="25%"><input type="text" name="nom" value="<?php echo stripslashes($donnees['nom']); ?>" maxlength="250" size="15"></td>
				<td width="45%"><?php echo stripslashes($donnees['description']); ?></td>
				<td  width="35%"><input type="hidden" name="id" value="<?php echo $donnees['id']; ?>" />
					<input type="submit" value="Editer" name="editer" class="bouton" />
					<input type="submit" value="Effacer" name="effacer" class="bouton" /></td>
				</tr>
			</form>
			<?php
		}
		$req->closeCursor();
		?>
		<tr><td colspan="3"><hr /></td></tr>
		<form method="post" action="index.php?page=admin_categories_creations">
			<tr><td></td>
			<td><input type="text" name="nom" value="" maxlength="250" size="15"></td>
			<td><input type="text" name="description" maxlength="250" size="40"></td>
			<td><input type="submit" value="Ajouter cette catégorie" name="ajouter" class="bouton" /></td>
			</tr>
		</form>
	</table>
	<?php
}
